clear all
set more off

program main
    import delimited "Snopes_results_url.txt", delimiter("|") clear varnames(1)
    gen day     = dofc(published_date * 1000 + mdyhms(1, 1, 1970, 0, 0, 0))
    gen yrmo    = ym(year(day), month(day))
    gen quarter = qofd(day)
    format yrmo %tm
    format quarter %tq
    keep if quarter >= q(2016q1) & quarter <= q(2018q3)
    count
    collapse (sum) facebook_engagement facebook_shares twitter_shares, by(quarter yrmo)
    collapse (mean) facebook_engagement facebook_shares twitter_shares, by(quarter)
    foreach var in facebook_engagement facebook_shares twitter_shares {
        replace `var' = `var' / 1000
    }
    gen ratio1 = facebook_engagement / twitter_shares /* engagements */
    gen ratio2 = facebook_shares     / twitter_shares /* shares */

    * Plot trends
    twoway line facebook_engagement quarter, graphregion(color(white)) ytitle("") ///
      title("Facebook Engagements (thousand)", size(5)) ylabel(0(3000)9000, angle(0)) ///
      xtitle("") tlabel(2016q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) name(fb_eng, replace)
    twoway line facebook_shares quarter, graphregion(color(white)) ytitle("") ///
      title("Facebook Shares (thousand)", size(5)) ylabel(0(600)1800, angle(0)) ///
      xtitle("") tlabel(2016q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) name(fb_share, replace)
    twoway line twitter_shares quarter, graphregion(color(white)) ytitle("") ///
      title("Twitter Shares (thousand)", size(5)) ylabel(0(40)120, angle(0)) ///
      xtitle("") tlabel(2016q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) name(tw, replace)
    twoway line ratio1 quarter, graphregion(color(white)) ///
      title("Facebook Engagements / Twitter Shares", size(5)) ytitle("") ylabel(0(60)180, angle(0)) ///
      xtitle("") tlabel(2016q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) name(ratio1, replace)
    twoway line ratio2 quarter, graphregion(color(white)) ///
      title("Facebook Shares / Twitter Shares", size(5)) ytitle("") ylabel(0(15)45, angle(0)) ///
      xtitle("") tlabel(2016q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) name(ratio2, replace) 

    * Combine graphs
    graph combine fb_eng tw ratio1, rows(3) graphregion(color(white)) xsize(3) ysize(7)
    graph export "TrendURL_eng.eps", replace
    graph combine fb_share tw ratio2, rows(3) graphregion(color(white)) xsize(3) ysize(7)
    graph export "TrendURL_share.eps", replace
end

main
